<?php
class QuanhuyensController extends AppController {

	var $name = 'Quanhuyens';

	function index($tinh_id=null) {
		$this->Quanhuyen->recursive = 0;
		$this->set('quanhuyens', $this->paginate(array('tinhtp_id'=>$tinh_id)));
        $tentinh=$this->Quanhuyen->query('select tentinhtp from tinhtps where id='.$tinh_id);
        $this->set(compact('tinh_id','tentinh'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid quanhuyen', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('quanhuyen', $this->Quanhuyen->read(null, $id));
	}

	function add($tinh_id) {
		if (!empty($this->data)) {
			$this->Quanhuyen->create();
			if ($this->Quanhuyen->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$tinh_id));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		$tinhtps = $this->Quanhuyen->Tinhtp->find('list');
		$this->set(compact('tinhtps','tinh_id'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid quanhuyen', true));
			$this->redirect(array('action' => 'index',$this->data['Quanhuyen'] ['tinhtp_id']));
		}
		if (!empty($this->data)) {
			if ($this->Quanhuyen->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$this->data['Quanhuyen'] ['tinhtp_id']));
			} else {
				$this->Session->setFlash(__('Thông tin không thể lưu', true),'error');
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Quanhuyen->read(null, $id);
		}
        $tinh_id=$this->data['Quanhuyen'] ['tinhtp_id'];
		$tinhtps = $this->Quanhuyen->Tinhtp->find('list',array('fields'=>'tentinhtp'));
		$this->set(compact('tinhtps','tinh_id'));
	}

	function delete($id = null,$tinh_id=null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for quanhuyen', true));
			$this->redirect(array('action'=>'index',$tinh_id));
		}
		if ($this->Quanhuyen->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$tinh_id));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true),'error');
		$this->redirect(array('action' => 'index',$tinh_id));
	}
}
